package pl.nith.battleships.windows.panels;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JPanel;

import pl.nith.battleships.helpers.BoardHelper;
import pl.nith.battleships.helpers.TileHelper;
import pl.nith.battleships.managers.Manager;
import pl.nith.battleships.models.Tile;
import pl.nith.battleships.resources.i18n.GameLocale;

public class BoardSurface extends JPanel
{
	private static final long serialVersionUID = -5962914418023528456L;
	public static boolean mouseOverBoard;
	public static boolean mouseOverBar;
	public static Point mousePos;
	
	private JButton setShip_ReadyButton;
	
	public BoardSurface()
	{
		super();
		
		initialize();
		
		setOpaque(true);
	}
	
	public void initialize()
	{
		addMouseListener(Manager.getMouseListener());
		addMouseMotionListener(Manager.getMouseListener());
		
		add(getSetShip_ReadyButton());
		
		mousePos = new Point();
	}
	
	public JButton getSetShip_ReadyButton()
	{
		if (setShip_ReadyButton == null)
		{
			setShip_ReadyButton = new JButton(GameLocale.getButtons().getString("nothing"));
			setShip_ReadyButton.addActionListener(Manager.getActionListener());
			setShip_ReadyButton.setEnabled(false);
			setShip_ReadyButton.setLocation(370, 260);
			setShip_ReadyButton.setSize(130, 60);
		}
		
		return setShip_ReadyButton;
	}
	
	public void disableButton()
	{
		setShip_ReadyButton.setEnabled(false);
	}
	
	public void enableButton()
	{
		setShip_ReadyButton.setEnabled(true);
	}
	
	public void hideButton()
	{
		setShip_ReadyButton.setVisible(false);
	}
	
	public void showButton()
	{
		setShip_ReadyButton.setVisible(true);
	}
	
	public void changeButtonToSetShips()
	{
		setShip_ReadyButton.setText(GameLocale.getButtons().getString("set_ships"));
		setShip_ReadyButton.setEnabled(true);
		setShip_ReadyButton.setActionCommand("SET_SHIPS");
	}
	
	public void changeButtonToReady()
	{
		setShip_ReadyButton.setText(GameLocale.getButtons().getString("ready"));
		setShip_ReadyButton.setEnabled(true);
		setShip_ReadyButton.setActionCommand("READY");
	}
	
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		drawBG(g);
		if (Manager.getBoard() != null)
		{
			drawPlayerBoard(g);
			drawHitBoard(g);
		}
		drawPlayerGrid(g);
		drawSeconPlayerGrid(g);
		drawLabels(g);
		drawBar(g);
		
		if (mouseOverBoard && !mouseOverBar)
			drawSelection(g);
		
		drawDebugInfo(g, "mousePos: " + mousePos.x + ", " + mousePos.y 
				+ " currentTile: " + TileHelper.getTileFromCoords(mousePos.x, mousePos.y).getRow() + ", "
				+ TileHelper.getTileFromCoords(mousePos.x, mousePos.y).getColumn());
		
		repaint();
	}
	
	public void drawBG(Graphics g)
	{
		g.setColor(Color.BLUE);
		g.fillRect(0, 0, getWidth(), getHeight());
	}
	
	public void drawPlayerGrid(Graphics g)
	{
		g.setColor(Color.WHITE);
		
		for (int row = 0; row <= BoardHelper.ROWS; row++)
			for (int col = 0; col <= BoardHelper.COLS; col++)
			{
				g.drawLine(0, (row * TileHelper.tileSize), BoardHelper.playerGridWidth, (row * TileHelper.tileSize));
				g.drawLine((col * TileHelper.tileSize), 0, (col * TileHelper.tileSize), BoardHelper.playerGridHeight);
			}
	}
	
	public void drawSeconPlayerGrid(Graphics g)
	{
		g.setColor(Color.ORANGE);
		
		for (int row = 0; row <= BoardHelper.ROWS; row++)
			for (int col = 0; col <= BoardHelper.COLS; col++)
			{
				g.drawLine(BoardHelper.gridShift, (row * TileHelper.tileSize), BoardHelper.playerGridWidth + BoardHelper.gridShift, (row * TileHelper.tileSize));
				g.drawLine((col * TileHelper.tileSize) + BoardHelper.gridShift, 0, (col * TileHelper.tileSize) + BoardHelper.gridShift, BoardHelper.playerGridHeight + BoardHelper.gridShift);
		}
	}
	
	public void drawPlayerBoard(Graphics g)
	{
		for (int row = 0; row < BoardHelper.ROWS; row++)
			for (int col = 0; col < BoardHelper.COLS; col++)
			{
				Rectangle tileBoundries = TileHelper.getTileBoundries(row, col, false);
				Tile currentTile = Manager.getBoard().getTile(row, col, false);
				
				switch (currentTile.getType())
				{
					case AC_START:
					{
						g.setColor(Color.LIGHT_GRAY);
						break;
					}
					
					case AC_BODY:
					{
						g.setColor(Color.LIGHT_GRAY);
						break;
					}
					
					case BS_START:
					{
						g.setColor(Color.CYAN);
						break;
					}
					
					case BS_BODY:
					{
						g.setColor(Color.CYAN);
						break;
					}
					
					case DS_START:
					{
						g.setColor(Color.ORANGE);
						break;
					}
					
					case DS_BODY:
					{
						g.setColor(Color.ORANGE);
						break;
					}
					
					case SM_START:
					{
						g.setColor(Color.MAGENTA);
						break;
					}
					
					case SM_BODY:
					{
						g.setColor(Color.MAGENTA);
						break;
					}
					
					case PB_START:
					{
						g.setColor(Color.PINK);
						break;
					}
					
					case PB_BODY:
					{
						g.setColor(Color.PINK);
						break;
					}
					
					case EMPTY:
					{
						g.setColor(Color.BLUE);
						break;
					}
					
					case HIT:
					{
						g.setColor(Color.RED);
						break;
					}
					
					case MISS:
					{
						g.setColor(Color.WHITE);
						break;
					}
					
					case SUNK:
					{
						g.setColor(Color.BLACK);
						break;
					}
				}

				g.fillRect(tileBoundries.x, tileBoundries.y, tileBoundries.width, tileBoundries.height);
			}
	}
	
	public void drawHitBoard(Graphics g)
	{
		for (int row = 0; row < BoardHelper.ROWS; row++)
			for (int col = 0; col < BoardHelper.COLS; col++)
			{
				Rectangle tileBoundries = TileHelper.getTileBoundries(row, col, true);
				Tile currentTile = Manager.getBoard().getTile(row, col, true);
				
				switch (currentTile.getType())
				{	
					case EMPTY:
					{
						g.setColor(Color.BLUE);
						break;
					}
					
					case HIT:
					{
						g.setColor(Color.RED);
						break;
					}
					
					case MISS:
					{
						g.setColor(Color.WHITE);
						break;
					}
					
					case SUNK:
					{
						g.setColor(Color.BLACK);
						break;
					}
				}

				g.fillRect(tileBoundries.x, tileBoundries.y, tileBoundries.width, tileBoundries.height);
			}
	}
	
	public void drawDebugInfo(Graphics g, String debugInfo)
	{
		g.setColor(Color.RED);
		g.drawString(debugInfo, BoardHelper.playerGridWidth + 10, 60);
		g.drawString(Manager.getGMS().currentGameState.toString(), BoardHelper.playerGridWidth + 10, 80);
		g.drawString(Manager.getGMS().turn.toString(), BoardHelper.playerGridWidth + 10, 100);
	}
	
	public void drawSelection(Graphics g)
	{
		Tile currentTile = TileHelper.getTileFromCoords(mousePos.x, mousePos.y);
		Rectangle selection = TileHelper.getTileBoundries(currentTile.getRow(), currentTile.getColumn(), currentTile.isOnHitBoard());
		
		g.setColor(Color.GREEN);
		g.drawRect(selection.x, selection.y, selection.width, selection.height);
		g.fillRect(selection.x, selection.y, selection.width, selection.height);
	}
	
	public void drawBar(Graphics g)
	{
		g.setColor(Color.DARK_GRAY);
		g.fill3DRect(BoardHelper.playerGridWidth + 1, 1, 160 - 1, BoardHelper.playerGridHeight + 1, true);
	}
	
	public void drawLabels(Graphics g)
	{
		// TODO Add real code to draw labels
	}
}
